package es.deusto.ingenieria.is.touringproject.formulation;


import java.util.ArrayList;

import es.deusto.ingenieria.is.search.formulation.State;

public class Environment extends State implements Cloneable {

	private int cities;
	private City start;
	private City end;
	private ArrayList <City> list= new ArrayList <City>();
	private ArrayList <City> visitedList= new ArrayList <City>();
	private double totalDistance;

	public Environment(int cities, City start, City end, double totalDistance) {
		super();
		this.cities = cities;
		this.start = start;
		this.end = end;
		this.totalDistance = totalDistance;
	}

	public Environment() {
		super();
	}

	public Environment(int cities, City start, City end, ArrayList<City> list) {
		super();
		this.cities = cities;
		this.start = start;
		this.end = end;
		this.list = list;
	}
	

	public double getTotalDistance() {
		return totalDistance;
	}

	public void setTotalDistance(double totalDistance) {
		this.totalDistance = totalDistance;
	}

	public Environment(int cities, City start, City end) {
		super();
		this.cities = cities;
		this.start = start;
		this.end = end;
	}


	public int getCities() {
		return cities;
	}

	public void setCities(int cities) {
		this.cities = cities;
	}

	public City getStart() {
		return start;
	}

	public void setStart(City start) {
		this.start = start;
	}

	public City getEnd() {
		return end;
	}

	public void setEnd(City end) {
		this.end = end;
	}

	public ArrayList<City> getList() {
		return list;
	}

	public ArrayList<City> getVisitedList() {
		return visitedList;
	}

	public void setVisitedList(ArrayList<City> visitedList) {
		this.visitedList = visitedList;
	}

	public void setList(ArrayList<City> list) {
		this.list = list;
	}

	public boolean equals(Object obj) {
		Environment env = (Environment) obj;
		return visitedList.equals(env.getVisitedList());
	}
	
	public String toString(){
		String lista ="";
		for(int i =0;i<visitedList.size();i++){
			lista+="\n"+visitedList.get(i).toString();
		}
		return lista;
	}

	public void addVisitedCity(City visited) {
		//A�ade la ciudad
		visitedList.add(visited);
	}

	@SuppressWarnings("unchecked")
	public Object clone() {
		Environment clone = new Environment(this.cities,this.start,this.end,this.totalDistance);
		clone.visitedList = ((ArrayList<City>) this.visitedList.clone());
		clone.list=(ArrayList<City>) this.list.clone();

		return clone;
	}
	
	
	public double getDistanciaFinal()
	{
		double dou;
		City temp= this.getVisitedList().get(this.getVisitedList().size()-1);
		double x= Math.pow(end.getCoordx()-temp.getCoordx(),2);
		double y= Math.pow(end.getCoordy()-temp.getCoordy(),2);
		dou=Math.sqrt(x + y);
		return dou;
	}
	
	
	public double getDistanciaManhattan()
	{
		double dou=0;
		double x= Math.abs(start.getCoordx()-this.getVisitedList().get(0).getCoordx());
		double y= Math.abs(start.getCoordy()-this.getVisitedList().get(0).getCoordy());
//		System.out.printf(start.getName());
//		System.out.printf("-");
//		System.out.printf(this.getVisitedList().get(0).getName());
		dou=x + y;
		for (int i=1;i<this.getVisitedList().size();i++)
		{
//			System.out.printf("-");
//			System.out.printf(this.getVisitedList().get(i).getName());

//

			x= Math.abs(this.getVisitedList().get(i).getCoordx()-this.getVisitedList().get(i-1).getCoordx());
			y= Math.abs(this.getVisitedList().get(i).getCoordy()-this.getVisitedList().get(i-1).getCoordy());

			dou+=x + y;
		}
//		System.out.println("");
		
//		double x= Math.abs(start.getCoordx()-this.getVisitedList().get(0).getCoordx());
//		double y= Math.abs(start.getCoordy()-this.getVisitedList().get(0).getCoordy());
		return dou;
	}
	
}